package com.pshcp.service;

import com.framework.util.SystemUtil;
import com.pshcp.domain.dto.DoctorSendMessageContentDTO;
import com.pshcp.domain.model.DoctorSendMessageContent;
import com.pshcp.mapper.DoctorSendMessageContentMapper;

import com.github.pagehelper.PageHelper;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CommonStatusEnum;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 医生群发消息内容表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class DoctorSendMessageContentService {

    @Autowired
    private DoctorSendMessageContentMapper doctorSendMessageContentMapper;

    public PageList<DoctorSendMessageContent> list(DoctorSendMessageContentDTO doctorSendMessageContentDTO) {
        if (doctorSendMessageContentDTO.getPageNum() != null && doctorSendMessageContentDTO.getPageSize() != null) {
            PageHelper.startPage(doctorSendMessageContentDTO.getPageNum(), doctorSendMessageContentDTO.getPageSize());
        }
        Example example = new Example(DoctorSendMessageContent.class);
        Example.Criteria criteria = example.createCriteria();
        if (doctorSendMessageContentDTO.getStatus() != null) {
            criteria.andEqualTo("status", doctorSendMessageContentDTO.getStatus());
        } else {
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        if (doctorSendMessageContentDTO.getDoctorId() != null) {
            criteria.andEqualTo("doctorId", doctorSendMessageContentDTO.getDoctorId());
        }
        example.setOrderByClause(" created_time desc");
        List<DoctorSendMessageContent> list = doctorSendMessageContentMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DoctorSendMessageContent> select(DoctorSendMessageContent doctorSendMessageContent) {
        return doctorSendMessageContentMapper.select(doctorSendMessageContent);
    }

    public Long insert(DoctorSendMessageContent doctorSendMessageContent) {
        doctorSendMessageContent.setId(SystemUtil.getId());
        doctorSendMessageContent.setStatus(CommonStatusEnum.INIT.getCode());
        doctorSendMessageContent.setCreatedTime(new Date());
        doctorSendMessageContentMapper.insert(doctorSendMessageContent);
        return doctorSendMessageContent.getId();
    }

    public int update(DoctorSendMessageContent doctorSendMessageContent) {
        doctorSendMessageContent.setUpdateTime(new Date());
        return doctorSendMessageContentMapper.updateByPrimaryKeySelective(doctorSendMessageContent);
    }

    public DoctorSendMessageContent findById(Long id) {
        return doctorSendMessageContentMapper.selectByPrimaryKey(id);
    }

    public List<DoctorSendMessageContent> findByIds(List<Long> ids) {
        Example example = new Example(DoctorSendMessageContent.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return doctorSendMessageContentMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(DoctorSendMessageContent.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DoctorSendMessageContent doctorSendMessageContent = new DoctorSendMessageContent();
        doctorSendMessageContent.setStatus(CommonStatusEnum.DEL.getCode());
        return doctorSendMessageContentMapper.updateByExampleSelective(doctorSendMessageContent, example);
    }

}
